<template>
  <div>
      <slot></slot>
  </div>
</template>

<script>
export default {
   name:'Form',
   props:{
       mode:{
           type:Object,
           default:()=>({})
       },
       rule:{
           type:Object,
           default:()=>({})
       }
   },
   methods:{
       validate(){
           const data=this.mode;//数据
              let flg=true;
              let errorMessage='';
           for(let key in data){
              if(this.rule[key] instanceof Array){
                 let rule=this.rule[key];
                for(let i=0;i<rule.length;i++){
                     flg=this._check(key,data[key],rule[i])
                     if(!flg){
                         errorMessage=rule[i].message;
                         break;
                     }
                }
                
               }else if(this.rule[key]){
                 flg=this._check(key,data[key],this.rule[key])
                //  errorMessage=flg;
                if(!flg){
                    errorMessage=this.rule[key].message;
                }
                }

                if(!flg){
                    break;
                }

           }

           if(flg){
               return Promise.resolve()
           }else{
               return Promise.reject(errorMessage)
           }
       },
       _check(ley,val,rule){//校验
            
                if(rule.required&&!val){
                    return false
                }else if(rule.reg&&!rule.reg.test(val)){
                    return false
                }else{
                     return true
                }
            

       }
   }

}
</script>

<style>

</style>